
var done=false;
var inprocess=false;


$(document).on('click', '.pcandidate', function(){

				if($(this).hasClass('pc_selected')){
					$(this).removeClass('pc_selected');
				}
				else{
					$(this).addClass('pc_selected');
				}
});




// helper function
function checkTokenSanityJS(){
	var token=$('#ptracker_token').val();
	if(token=="")
		return false;
	return true;
}



function getProjects(){
	if(!checkTokenSanityJS()){
		alert("Invalid token . Please try again");
		return false;
	}

	if(done||inprocess){
		alert("This action is in process or has been done.");
		return false;
	}
	inprocess=true;
	 var ptracker_token=$('#ptracker_token').val();
	 var github_token  =$('#github_token').val();
	 console.log("gtoken,"+github_token);
     $.ajax({
          type: "GET",
          url: "/user/get_projects",
          dataType: "script" ,
          data: {ptracker_token: ptracker_token , github_token:github_token}
     }).success(function(data){ 
     	if(!server_sanity){
     		$( "#2to1" ).trigger( "click" );
     		done=false;
		}
		else{
			done=true;
		}
		inprocess=false;
     });


}


function sendSelectedProjects(){
	var arr=[];
	var project="";
	$('.pc_selected').each(function( index ) {
		console.log( index + ": " + $( this ).attr('pid') );
		arr[index]=$(this).attr('pid');
	});
	var msg= JSON.stringify(arr);
	// alert(msg);
	$.ajax({
          type: "GET",
          url: "/user/process_selected_projects",
          dataType: "script" ,
          data: {msg:msg}
    }).success(function(data){ 
   	});

}


function addPtrackerToken(){
	if(!checkTokenSanityJS()){
		alert("Invalid token . Please try again");
		return false;
	}

	if(done||inprocess){
		alert("This action is in process or has been done.");
		return false;
	}
	inprocess=true;
	 var ptracker_token=$('#ptracker_token').val();
	 var github_token  =$('#github_token').val();
     $.ajax({
          type: "GET",
          url: "/user/add_ptracker_token",
          dataType: "script" ,
          data: {ptracker_token: ptracker_token , github_token:github_token}
     }).success(function(data){ 
     	if(!server_sanity){
     		$( "#2to1" ).trigger( "click" );
     		done=false;
		}
		else{
			done=true;
		}
		inprocess=false;
     });


}










// tips popover 
